/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tesoreria.converters;

import org.cip.esymons.general.util.ControladorAbstracto;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.cip.esymons.eventos.be.Ambiente;
import org.cip.esymons.tesoreria.beans.AmbienteTesoreriaController;

/**
 *
 * @author Ingrid Capuñay
 */
public class AmbienteTesoreriaConverter implements Converter{

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null) {
            return null;
        }      

        try {
            Integer id = new Integer(value);
            AmbienteTesoreriaController controller = (AmbienteTesoreriaController) ControladorAbstracto.getBean("ambienteTesoreriaController");
            return controller.buscarPorCodigo(id);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Ambiente) {
            Ambiente o = (Ambiente) object;
            return "" + o.getAmbCodigo();
        } else {
            throw new IllegalArgumentException("object:" + object + " of type:" + object.getClass().getName() + "; expected type: be.Ambiente");
        }
    }

}
